using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace daq_test_cs
{
    public enum DeviceType
    {
        DAQ125,
        DAQ206,
        DAQ207,
        DAQ216,
        DAQ217,
        DAQ322
    }

    public class DeviceManager
    {
        private IntPtr device;
        private IntPtr usbServer;
        private IntPtr tcpServer; // TCP服务器实例
        private DeviceType deviceType;
        private bool isConnected = false;
        private string tcpServerIP = "192.168.1.100"; // 默认TCP服务器IP
        private ushort tcpServerPort = 8899; // 默认TCP服务器端口

        public DeviceManager(DeviceType type)
        {
            deviceType = type;
            InitializeDevice();
        }

        public DeviceManager(DeviceType type, string ip = null, ushort port = 0)
        {
            deviceType = type;
            
            // 如果提供了IP和端口，则使用提供的值
            if (!string.IsNullOrEmpty(ip))
            {
                tcpServerIP = ip;
            }
           
            if (port > 0)
            {
                tcpServerPort = port;
            }
            
            InitializeDevice();
        }

        private void InitializeDevice()
        {
            // 创建设备实例
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    device = DAQ125Wrapper.DAQ125_New();
                    usbServer = USBServerWrapper.USBServer_New(USBServerWrapper.DeviceType.DeviceType_DAQ125);
                    break;
                case DeviceType.DAQ206:
                    device = DAQ206Wrapper.DAQ206_New();
                    usbServer = USBServerWrapper.USBServer_New(USBServerWrapper.DeviceType.DeviceType_DAQ206);
                    break;
                case DeviceType.DAQ207:
                    device = DAQ207Wrapper.DAQ207_New();
                    usbServer = USBServerWrapper.USBServer_New(USBServerWrapper.DeviceType.DeviceType_DAQ207);
                    break;
                case DeviceType.DAQ216:
                    device = DAQ216Wrapper.DAQ216_New();
                    usbServer = USBServerWrapper.USBServer_New(USBServerWrapper.DeviceType.DeviceType_DAQ216);
                    break;
                case DeviceType.DAQ217:
                    device = DAQ217Wrapper.DAQ217_New();
                    usbServer = USBServerWrapper.USBServer_New(USBServerWrapper.DeviceType.DeviceType_DAQ217);
                    break;
                case DeviceType.DAQ322:
                    Console.WriteLine($"正在创建 {deviceType} 设备");
                    device = DAQ322Wrapper.DAQ322_New();
                    tcpServer = TCPServerWrapper.TcpServer_New(tcpServerIP, tcpServerIP.Length, tcpServerPort);
                    if (tcpServer == IntPtr.Zero)
                    {
                        throw new Exception($"无法创建 {deviceType} TCP服务器");
                    }
                    return;
            }

            if (usbServer == IntPtr.Zero)
            {
                throw new Exception($"无法创建 {deviceType} USB服务器");
            }
        }

        public bool ConnectDevice()
        {
            try
            {
                // DAQ322使用TCP服务器连接
                if (deviceType == DeviceType.DAQ322)
                {
                    return ConnectDAQ322Device();
                }
                
                // 其他设备使用USB服务器连接
                // 使用新的API接口
                USBServerWrapper.USBServer_StartSearch(usbServer);
                
                // 等待设备搜索完成
                Thread.Sleep(1000);
                
                uint deviceCount = USBServerWrapper.USBServer_GetDeviceCount(usbServer);
                
                if (deviceCount == 0)
                {
                    Console.WriteLine($"未发现 {deviceType} 设备");
                    return false;
                }

                Console.WriteLine($"发现 {deviceCount} 个 {deviceType} 设备");

                // 按照API文档的多设备连接最佳实践，始终使用索引0
                for (uint i = 0; i < deviceCount; i++)
                {
                    var socket = USBServerWrapper.USBServer_GetClientByIndex(usbServer, 0); // 始终使用索引0
                    if (socket == IntPtr.Zero) continue;

                    bool useBackendResult = false;
                    bool initResult = false;
                    bool connectResult = false;

                    // 根据设备类型调用相应的方法
                    switch (deviceType)
                    {
                        case DeviceType.DAQ125:
                            useBackendResult = DAQ125Wrapper.DAQ125_UseBackend(device, socket);
                            if (useBackendResult) initResult = DAQ125Wrapper.DAQ125_InitializeDevice(device);
                            if (initResult) connectResult = DAQ125Wrapper.DAQ125_ConnectedDevice(device);
                            break;
                        case DeviceType.DAQ206:
                            useBackendResult = DAQ206Wrapper.DAQ206_UseBackend(device, socket);
                            if (useBackendResult) initResult = DAQ206Wrapper.DAQ206_InitializeDevice(device);
                            if (initResult) connectResult = DAQ206Wrapper.DAQ206_ConnectedDevice(device);
                            break;
                        case DeviceType.DAQ207:
                            useBackendResult = DAQ207Wrapper.DAQ207_UseBackend(device, socket);
                            if (useBackendResult) initResult = DAQ207Wrapper.DAQ207_InitializeDevice(device);
                            if (initResult) connectResult = DAQ207Wrapper.DAQ207_ConnectedDevice(device);
                            break;
                        case DeviceType.DAQ216:
                            useBackendResult = DAQ216Wrapper.DAQ216_UseBackend(device, socket);
                            if (useBackendResult) initResult = DAQ216Wrapper.DAQ216_InitializeDevice(device);
                            if (initResult) connectResult = DAQ216Wrapper.DAQ216_ConnectedDevice(device);
                            break;
                        case DeviceType.DAQ217:
                            useBackendResult = DAQ217Wrapper.DAQ217_UseBackend(device, socket);
                            if (useBackendResult) initResult = DAQ217Wrapper.DAQ217_InitializeDevice(device);
                            if (initResult) connectResult = DAQ217Wrapper.DAQ217_ConnectedDevice(device);
                            break;
                    }

                    if (connectResult)
                    {
                        Console.WriteLine($"成功连接到 {deviceType} 设备");
                        isConnected = true;
                        return true;
                    }
                }

                Console.WriteLine($"无法连接到任何 {deviceType} 设备");
                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"连接 {deviceType} 设备时发生错误: {ex.Message}");
                return false;
            }
        }
        
        // DAQ322设备使用TCP服务器连接
        private bool ConnectDAQ322Device()
        {
            Console.WriteLine($"正在连接TCP服务器 {tcpServerIP}:{tcpServerPort}");
            
            // 开始接受客户端连接，设置超时时间为10秒，异步模式，最大客户端数为1
            TCPServerWrapper.TcpServer_StartAccept(tcpServer, 10000, true, 1);
            
            // 等待连接完成
            int timeout = 10; // 10秒超时
            while (!TCPServerWrapper.TcpServer_IsAcceptComplete(tcpServer) && timeout > 0)
            {
                Thread.Sleep(1000);
                timeout--;
            }
            
            if (timeout <= 0)
            {
                Console.WriteLine("TCP服务器连接超时");
                return false;
            }
            
            // 获取已连接的客户端数量和IP
            byte clientCount = 0;
            IntPtr clientsIPPtr = TCPServerWrapper.TcpServer_GetClientsIP(tcpServer, ref clientCount);
            
            if (clientCount == 0)
            {
                Console.WriteLine("未发现已连接的TCP客户端");
                return false;
            }
            
            // 获取客户端IP列表
            List<string> clientIPs = TCPServerWrapper.GetStringListFromCharPtrArray(clientsIPPtr, clientCount);
            Console.WriteLine($"发现 {clientCount} 个TCP客户端: {string.Join(", ", clientIPs)}");
            
            // 获取第一个客户端连接
            string firstClientIP = clientIPs[0];
            var socket = TCPServerWrapper.TcpServer_GetClient(tcpServer, firstClientIP, firstClientIP.Length);
            
            if (socket == IntPtr.Zero)
            {
                Console.WriteLine($"无法获取TCP客户端连接: {firstClientIP}");
                return false;
            }
            
            // 使用TCP客户端连接DAQ322设备
            bool useBackendResult = DAQ322Wrapper.DAQ322_UseBackend(device, socket);
            if (!useBackendResult)
            {
                Console.WriteLine("DAQ322设备使用TCP后端失败");
                return false;
            }
            
            bool initResult = DAQ322Wrapper.DAQ322_InitializeDevice(device);
            if (!initResult)
            {
                Console.WriteLine("DAQ322设备初始化失败");
                return false;
            }
            
            bool connectResult = DAQ322Wrapper.DAQ322_ConnectedDevice(device);
            if (!connectResult)
            {
                Console.WriteLine("DAQ322设备连接失败");
                return false;
            }
            
            Console.WriteLine($"成功连接到DAQ322设备，TCP客户端IP: {firstClientIP}");
            isConnected = true;
            return true;
        }

        public bool ConfigureDevice(int sampleRate = 500000, int voltage = 7, uint triggerLength = 49600, byte? channelMask = null)
        {
            if (!isConnected)
            {
                Console.WriteLine("设备未连接，无法配置");
                return false;
            }

            try
            {
                bool result = true;

                // 根据设备类型设置默认通道掩码
                if (channelMask == null)
                {
                    switch (deviceType)
                    {
                        case DeviceType.DAQ125:
                        case DeviceType.DAQ322:
                            channelMask = 0b11111111; // 8通道全开
                            break;
                        case DeviceType.DAQ206:
                        case DeviceType.DAQ207:
                            channelMask = 0b00000011; // 2通道全开
                            break;
                        case DeviceType.DAQ216:
                        case DeviceType.DAQ217:
                            channelMask = 0b00001111; // 4通道全开
                            break;
                        default:
                            channelMask = 0b00001111; // 默认4通道
                            break;
                    }
                }

                Console.WriteLine($"{deviceType} 使用通道掩码: 0b{Convert.ToString(channelMask.Value, 2).PadLeft(8, '0')}");

                // 配置触发模式
                if(deviceType == DeviceType.DAQ322)
                {
                    result &= ConfigADCTrigger(0, 1, 0, 300); // 滚动模式，目前DAQ322只支持滚动模式采集
                }
                else
                {
                    result &= ConfigADCTrigger(1, 1, 0, 300); // 上升沿触发，普通模式，软件触发
                }

                // 配置采样率
                if(deviceType == DeviceType.DAQ322)
                {
                    result &= ConfigADCSampleRate(200000);
                }
                else
                {
                    result &= ConfigADCSampleRate(sampleRate);
                }

                // 配置电压量程
                result &= ConfigADCVoltage(voltage);

                // 配置触发长度
                result &= ConfigADCTriggerLength(triggerLength);

                // 配置触发超时
                result &= ConfigTriggerTimeout(100);

                // 配置ADC通道
                result &= ConfigADCChannel(channelMask.Value);
                // 配置ACDC模式（仅支持ACDC的设备）
                if (deviceType == DeviceType.DAQ206 || deviceType == DeviceType.DAQ207 || 
                    deviceType == DeviceType.DAQ216 || deviceType == DeviceType.DAQ217)
                {
                    result &= ConfigACDC(1);
                }

                if (result)
                {
                    Console.WriteLine($"{deviceType} 设备配置成功");
                }
                else
                {
                    Console.WriteLine($"{deviceType} 设备配置失败");
                }

                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"配置 {deviceType} 设备时发生错误: {ex.Message}");
                return false;
            }
        }

        public bool StartCollection()
        {
            if (!isConnected) return false;

            bool result = false;
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    result = DAQ125Wrapper.DAQ125_StartCollection(device);
                    break;
                case DeviceType.DAQ206:
                    result = DAQ206Wrapper.DAQ206_StartCollection(device);
                    break;
                case DeviceType.DAQ207:
                    result = DAQ207Wrapper.DAQ207_StartCollection(device);
                    break;
                case DeviceType.DAQ216:
                    result = DAQ216Wrapper.DAQ216_StartCollection(device);
                    break;
                case DeviceType.DAQ217:
                    result = DAQ217Wrapper.DAQ217_StartCollection(device);
                    break;
                case DeviceType.DAQ322:
                    result = DAQ322Wrapper.DAQ322_StartCollection(device);
                    break;
            }

            if (result)
            {
                Console.WriteLine($"{deviceType} 开始采集");
            }
            else
            {
                Console.WriteLine($"{deviceType} 开始采集失败");
            }

            return result;
        }

        public bool StopCollection()
        {
            if (!isConnected) return false;

            bool result = false;
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    result = DAQ125Wrapper.DAQ125_StopCollection(device);
                    break;
                case DeviceType.DAQ206:
                    result = DAQ206Wrapper.DAQ206_StopCollection(device);
                    break;
                case DeviceType.DAQ207:
                    result = DAQ207Wrapper.DAQ207_StopCollection(device);
                    break;
                case DeviceType.DAQ216:
                    result = DAQ216Wrapper.DAQ216_StopCollection(device);
                    break;
                case DeviceType.DAQ217:
                    result = DAQ217Wrapper.DAQ217_StopCollection(device);
                    break;
                case DeviceType.DAQ322:
                    result = DAQ322Wrapper.DAQ322_StopCollection(device);
                    break;
            }

            if (result)
            {
                Console.WriteLine($"{deviceType} 停止采集");
            }
            else
            {
                Console.WriteLine($"{deviceType} 停止采集失败");
            }

            return result;
        }

        public bool TriggerSoftware()
        {
            if (!isConnected) return false;

            bool result = false;
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    result = DAQ125Wrapper.DAQ125_ConfigADCTriggerSource(device);
                    break;
                case DeviceType.DAQ206:
                    result = DAQ206Wrapper.DAQ206_ConfigADCTriggerSource(device);
                    break;
                case DeviceType.DAQ207:
                    result = DAQ207Wrapper.DAQ207_ConfigADCTriggerSource(device);
                    break;
                case DeviceType.DAQ216:
                    result = DAQ216Wrapper.DAQ216_ConfigADCTriggerSource(device);
                    break;
                case DeviceType.DAQ217:
                    result = DAQ217Wrapper.DAQ217_ConfigADCTriggerSource(device);
                    break;
                case DeviceType.DAQ322:
                    result = DAQ322Wrapper.DAQ322_ConfigADCTriggerSource(device);
                    break;
            }

            return result;
        }

        public bool ReadData(byte channel, float[] buffer, uint size)
        {
            if (!isConnected) return false;

            bool result = false;
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    result = DAQ125Wrapper.DAQ125_TryReadData_Batch(device, channel, buffer, size);
                    break;
                case DeviceType.DAQ206:
                    result = DAQ206Wrapper.DAQ206_TryReadData_Batch(device, channel, buffer, size);
                    break;
                case DeviceType.DAQ207:
                    result = DAQ207Wrapper.DAQ207_TryReadData_Batch(device, channel, buffer, size);
                    break;
                case DeviceType.DAQ216:
                    result = DAQ216Wrapper.DAQ216_TryReadData_Batch(device, channel, buffer, size);
                    break;
                case DeviceType.DAQ217:
                    result = DAQ217Wrapper.DAQ217_TryReadData_Batch(device, channel, buffer, size);
                    break;
                case DeviceType.DAQ322:
                    result = DAQ322Wrapper.DAQ322_TryReadData_Batch(device, channel, buffer, size);
                    break;
            }

            return result;
        }

        private bool ConfigADCTrigger(int type, int mode, int channel, int level)
        {
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    return DAQ125Wrapper.DAQ125_ConfigADCTrigger(device, type, mode, channel, level);
                case DeviceType.DAQ206:
                    return DAQ206Wrapper.DAQ206_ConfigADCTrigger(device, type, mode, channel, level);
                case DeviceType.DAQ207:
                    return DAQ207Wrapper.DAQ207_ConfigADCTrigger(device, type, mode, channel, level);
                case DeviceType.DAQ216:
                    return DAQ216Wrapper.DAQ216_ConfigADCTrigger(device, type, mode, channel, level);
                case DeviceType.DAQ217:
                    return DAQ217Wrapper.DAQ217_ConfigADCTrigger(device, type, mode, channel, level);
                case DeviceType.DAQ322:
                    return DAQ322Wrapper.DAQ322_ConfigADCTrigger(device, type, mode, channel, level);
                default:
                    return false;
            }
        }

        private bool ConfigADCSampleRate(int sampleRate)
        {
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    return DAQ125Wrapper.DAQ125_ConfigADCSampleRate(device, sampleRate);
                case DeviceType.DAQ206:
                    return DAQ206Wrapper.DAQ206_ConfigADCSampleRate(device, sampleRate);
                case DeviceType.DAQ207:
                    return DAQ207Wrapper.DAQ207_ConfigADCSampleRate(device, sampleRate);
                case DeviceType.DAQ216:
                    return DAQ216Wrapper.DAQ216_ConfigADCSampleRate(device, sampleRate);
                case DeviceType.DAQ217:
                    return DAQ217Wrapper.DAQ217_ConfigADCSampleRate(device, sampleRate);
                case DeviceType.DAQ322:
                    return DAQ322Wrapper.DAQ322_ConfigADCSampleRate(device, sampleRate);
                default:
                    return false;
            }
        }

        private bool ConfigADCVoltage(int voltage)
        {
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    return DAQ125Wrapper.DAQ125_ConfigADCVoltage(device, voltage);
                case DeviceType.DAQ206:
                    return DAQ206Wrapper.DAQ206_ConfigADCVoltage(device, voltage);
                case DeviceType.DAQ207:
                    return DAQ207Wrapper.DAQ207_ConfigADCVoltage(device, voltage);
                case DeviceType.DAQ216:
                    return DAQ216Wrapper.DAQ216_ConfigADCVoltage(device, voltage);
                case DeviceType.DAQ217:
                    return DAQ217Wrapper.DAQ217_ConfigADCVoltage(device, voltage);
                case DeviceType.DAQ322:
                    return DAQ322Wrapper.DAQ322_ConfigADCVoltage(device, voltage);
                default:
                    return false;
            }
        }

        private bool ConfigADCTriggerLength(uint length)
        {
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    return DAQ125Wrapper.DAQ125_ConfigADCTriggerLength(device, length);
                case DeviceType.DAQ206:
                    return DAQ206Wrapper.DAQ206_ConfigADCTriggerLength(device, length);
                case DeviceType.DAQ207:
                    return DAQ207Wrapper.DAQ207_ConfigADCTriggerLength(device, length);
                case DeviceType.DAQ216:
                    return DAQ216Wrapper.DAQ216_ConfigADCTriggerLength(device, length);
                case DeviceType.DAQ217:
                    return DAQ217Wrapper.DAQ217_ConfigADCTriggerLength(device, length);
                case DeviceType.DAQ322:
                    return DAQ322Wrapper.DAQ322_ConfigADCTriggerLength(device, length);
                default:
                    return false;
            }
        }

        private bool ConfigTriggerTimeout(uint timeout)
        {
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    return DAQ125Wrapper.DAQ125_ConfigTriggerTimeout(device, timeout);
                case DeviceType.DAQ206:
                    return DAQ206Wrapper.DAQ206_ConfigTriggerTimeout(device, timeout);
                case DeviceType.DAQ207:
                    return DAQ207Wrapper.DAQ207_ConfigTriggerTimeout(device, timeout);
                case DeviceType.DAQ216:
                    return DAQ216Wrapper.DAQ216_ConfigTriggerTimeout(device, timeout);
                case DeviceType.DAQ217:
                    return DAQ217Wrapper.DAQ217_ConfigTriggerTimeout(device, timeout);
                case DeviceType.DAQ322:
                    return DAQ322Wrapper.DAQ322_ConfigTriggerTimeout(device, timeout);
                default:
                    return false;
            }
        }

        private bool ConfigADCChannel(byte channel)
        {
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    return DAQ125Wrapper.DAQ125_ConfigADCChannel(device, channel);
                case DeviceType.DAQ206:
                    return DAQ206Wrapper.DAQ206_ConfigADCChannel(device, channel);
                case DeviceType.DAQ207:
                    return DAQ207Wrapper.DAQ207_ConfigADCChannel(device, channel);
                case DeviceType.DAQ216:
                    return DAQ216Wrapper.DAQ216_ConfigADCChannel(device, channel);
                case DeviceType.DAQ217:
                    return DAQ217Wrapper.DAQ217_ConfigADCChannel(device, channel);
                case DeviceType.DAQ322:
                    return DAQ322Wrapper.DAQ322_ConfigADCChannel(device, channel);
                default:
                    return false;
            }
        }

        private bool ConfigACDC(byte mode)
        {
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                    return DAQ125Wrapper.DAQ125_ConfigACDC(device, mode);
                case DeviceType.DAQ206:
                    return DAQ206Wrapper.DAQ206_ConfigACDC(device, mode);
                case DeviceType.DAQ207:
                    return DAQ207Wrapper.DAQ207_ConfigACDC(device, mode);
                case DeviceType.DAQ216:
                    return DAQ216Wrapper.DAQ216_ConfigACDC(device, mode);
                case DeviceType.DAQ217:
                    return DAQ217Wrapper.DAQ217_ConfigACDC(device, mode);
                default:
                    return false;
            }
        }

        /// <summary>
        /// 获取设备的通道数量
        /// </summary>
        /// <returns>设备支持的通道数量</returns>
        public int GetChannelCount()
        {
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                case DeviceType.DAQ322:
                    return 8; // DAQ125、DAQ322为8通道
                case DeviceType.DAQ206:
                case DeviceType.DAQ207:
                    return 2; // DAQ206、DAQ207为2通道
                case DeviceType.DAQ216:
                case DeviceType.DAQ217:
                    return 4; // DAQ216、DAQ217为4通道
                default:
                    return 4; // 默认4通道
            }
        }

        /// <summary>
        /// 获取设备的默认通道掩码
        /// </summary>
        /// <returns>默认通道掩码</returns>
        public byte GetDefaultChannelMask()
        {
            switch (deviceType)
            {
                case DeviceType.DAQ125:
                case DeviceType.DAQ322:
                    return 0b11111111; // 8通道全开
                case DeviceType.DAQ206:
                case DeviceType.DAQ207:
                    return 0b00000011; // 2通道全开
                case DeviceType.DAQ216:
                case DeviceType.DAQ217:
                    return 0b00001111; // 4通道全开
                default:
                    return 0b00001111; // 默认4通道
            }
        }

        /// <summary>
        /// 设置GPIO状态（仅DAQ322支持）
        /// </summary>
        /// <param name="gpioState">GPIO状态数组</param>
        /// <returns>设置是否成功</returns>
        public bool SetGPIOState(byte[] gpioState)
        {
            if (deviceType != DeviceType.DAQ322)
            {
                Console.WriteLine($"{deviceType} 不支持GPIO功能");
                return false;
            }

            if (!isConnected) return false;

            return DAQ322Wrapper.DAQ322_SetGPI(device, gpioState, gpioState.Length);
        }

        /// <summary>
        /// 获取GPIO状态（仅DAQ322支持）
        /// </summary>
        /// <param name="gpioState">用于存储GPIO状态的数组</param>
        /// <returns>获取是否成功</returns>
        public bool GetGPIOState(byte[] gpioState)
        {
            if (deviceType != DeviceType.DAQ322)
            {
                Console.WriteLine($"{deviceType} 不支持GPIO功能");
                return false;
            }

            if (!isConnected) return false;

            // 发送获取GPIO状态命令
            bool result = DAQ322Wrapper.DAQ322_GetGPI(device);
            if (!result) return false;

            // 获取GPIO状态值
            int arraySize = gpioState.Length;
            return DAQ322Wrapper.DAQ322_GetGPIOState(device, gpioState, ref arraySize);
        }

        // 获取设备实例
        public IntPtr GetDevice()
        {
            return device;
        }

        public void Dispose()
        {
            if (device != IntPtr.Zero)
            {
                switch (deviceType)
                {
                    case DeviceType.DAQ125:
                        DAQ125Wrapper.DAQ125_Delete(device);
                        break;
                    case DeviceType.DAQ206:
                        DAQ206Wrapper.DAQ206_Delete(device);
                        break;
                    case DeviceType.DAQ207:
                        DAQ207Wrapper.DAQ207_Delete(device);
                        break;
                    case DeviceType.DAQ216:
                        DAQ216Wrapper.DAQ216_Delete(device);
                        break;
                    case DeviceType.DAQ217:
                        DAQ217Wrapper.DAQ217_Delete(device);
                        break;
                    case DeviceType.DAQ322:
                        DAQ322Wrapper.DAQ322_Delete(device);
                        break;
                }
                device = IntPtr.Zero;
            }

            if (usbServer != IntPtr.Zero)
            {
                USBServerWrapper.USBServer_Delete(usbServer);
                usbServer = IntPtr.Zero;
            }
            
            if (tcpServer != IntPtr.Zero)
            {
                TCPServerWrapper.TcpServer_Delete(tcpServer);
                tcpServer = IntPtr.Zero;
            }

            isConnected = false;
        }
    }
}